package client.messages;

import java.awt.Color;
import java.awt.Component;
import java.awt.Image;

import javax.swing.DefaultListCellRenderer;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.ListCellRenderer;

import miscellaneous.Storage;

import data.Invite;
import data.Message;

public class MessageListRenderer extends DefaultListCellRenderer implements ListCellRenderer {

	private ImageIcon warningIcon = new ImageIcon("resources/icons/warning.gif");
	private JLabel label;

	public MessageListRenderer() {
		super();
		ajustImageSize();
	}

	@Override
	public Component getListCellRendererComponent(JList list, Object message,
			int index, boolean isSelected, boolean cellHasFocus) {

		if(message == null)
			label = new JLabel("nullll");
		else
			label = (JLabel) super.getListCellRendererComponent(list, message, index, isSelected, cellHasFocus);

		
		Message msg = (Message) message;
		label.setForeground(Color.BLACK);

		if(msg.getReadTime() == null || msg.getReadTime().getTimeInMillis() == 0 ) {
			label.setIcon(warningIcon);
		}

		if(msg instanceof Invite) {
			label.setBackground(Storage.colorMeetingFillAnswered);
		} else {
			label.setBackground(Storage.colorAppointmentFill);
			
		}

		return label;
	}

	public void ajustImageSize() {
		int height = 35;
		int width = 35;

		Image tempImage;

		tempImage = warningIcon.getImage();
		warningIcon = new ImageIcon(tempImage.getScaledInstance((int)(width*0.3), (int)(height*0.3), 0));
	}
}
